
define(function (require, exports, module) {
	WebPDF.DrawingTools = {};
	var CPDF_Point = WebPDF.CPDF_Point;
    var AnnotFactory = WebPDF.AnnotFactory;
	WebPDF.DrawingTools.DA_BEZIER = 0.5522847498308;
    WebPDF.DrawingTools.Pencil = function (pReaderApp,pBaseAnnotPlugin,cavans,width,color,hexColor) {
        var _pReaderApp = pReaderApp,         
			_cavans = cavans,
			_pBaseAnnotPlugin = pBaseAnnotPlugin,
			_width = width,
			_color = color,
			_hexColor = hexColor,
			_pointArray = [],
			_minX = 0,
			_maxX = 0,
			_minY = 0,
			_maxY = 0,
			_pPageView = null,
			_bMouseDown = false,
			_self = this;

		var DEFAULT_OPCITY = 1.0;
        var MKA_POPUPWIDTH = 180;
        var MKA_POPUPHEIGHT = 120;

		var _cavans2DContext = _cavans.getContext('2d');

		this.OnMouseDown = function(e,pPageView)
		{
			_pPageView = pPageView;
			_cavans2DContext.strokeStyle = _color;
			_cavans2DContext.lineWidth = _width * pPageView.getScale();
			var offset = $(_cavans).offset();
			var sX = e.pageX - offset.left;
			var sY = e.pageY - offset.top;
			_cavans2DContext.beginPath();
			_cavans2DContext.moveTo(sX,sY);
			_minX = sX;
			_maxX = sX;
			_minY = sY;
			_maxY = sY;
			var pt = new WebPDF.PDFPoint(sX,sY);
			_pointArray = [];
			_pointArray.push(pt);
			_bMouseDown = true;

			return true;
		};


		this.OnMouseMove = function(e)
		{
			if(_bMouseDown)
			{
				var pageX = e.pageX;
				var pageY = e.pageY;
				var pageOffsetHtmlDoc = $("#" + _pPageView.getPageViewContainerID()).offset();
				var offset = $(_cavans).offset();
				var ptOffsetPageX = pageX - pageOffsetHtmlDoc.left;
				var ptOffsetPageY = pageY - pageOffsetHtmlDoc.top;
				if(ptOffsetPageX < 0)
					pageX = pageOffsetHtmlDoc.left;
				if(ptOffsetPageX > _pPageView.getPageViewWidth())
					pageX = pageOffsetHtmlDoc.left+_pPageView.getPageViewWidth();
				if(ptOffsetPageY < 0)
					pageY = pageOffsetHtmlDoc.top;
				if(ptOffsetPageY > _pPageView.getPageViewHeight())
					pageY = pageOffsetHtmlDoc.top+_pPageView.getPageViewHeight();
				var eX = pageX - offset.left;
				var eY = pageY - offset.top;
				_cavans2DContext.lineTo(eX,eY);
				_cavans2DContext.stroke();
				_cavans2DContext.moveTo(eX,eY);
				if(eX < _minX)
					_minX = eX;
				if(eX > _maxX)
					_maxX = eX;
				if(eY < _minY)
					_minY = eY;
				if(eY > _maxY)
					_maxY = eY;
				var pt = new WebPDF.PDFPoint(eX,eY);
				_pointArray.push(pt);
				return true;
			}
			return true;
		};

		function _VectorLength(point)
		{
			return Math.sqrt(point.x * point.x + point.y * point.y);
		}

		function _SlopeAngle(point)
		{
			return _ArcCosine(point,new CPDF_Point(1.0, 0.0));
		}	

		function _ArcCosine(pointA, pointB)
		{
			return Math.acos(_Cosine(pointA, pointB));
		}

		function _Cosine(pointA, pointB)
		{
			var dotProduct = _DotProduct(pointA,pointB);
			return dotProduct / (_VectorLength(pointA) * _VectorLength(pointB));
		}

		function _DotProduct(pointA, pointB)
		{ 
			return pointA.x * pointB.x + pointA.y * pointB.y; 
		}

		function DA_GETSIGN(a){
			return (a > 0 ? 1 : a < 0 ? -1 : 0);
		}

		this.CreateAnnot = function () {
			var ptCount = _pointArray.length;
			if(ptCount <=0 )
				return;
			var cavansOffset = $(_cavans).offset();
			var pageOffsetHtmlDoc = $("#" + _pPageView.getPageViewContainerID()).offset();
			var inkArray = [];
			var pdfPtArray = [];
			var fDevitionX = 1.5;
			var fDevitionY = 1.5;
			if(ptCount == 1)
			{
					var firstPt = _pointArray[0];
					_pointArray = [];
					var pDocView = _pPageView.getDocView();
					var nScale = _pReaderApp.getPixelsPerPoint()* pDocView.getScale();
					for(var j = 1;j<=_width;j++)
					{
						var k = (j - WebPDF.DrawingTools.DA_BEZIER) * nScale;
						_minX = (firstPt.x -k) < _minX ? (firstPt.x -k):_minX;
						_minX = (firstPt.x +k) < _minX ? (firstPt.x +k):_minX;
						_maxX = (firstPt.x -k) > _maxX ? (firstPt.x -k):_maxX;
						_maxX = (firstPt.x +k) > _maxX ? (firstPt.x +k):_maxX;
						_minY = (firstPt.y -k) < _minY ? (firstPt.y -k):_minY;
						_minY = (firstPt.y +k) < _minY ? (firstPt.y +k):_minY;
						_maxY = (firstPt.y -k) > _maxY ? (firstPt.y -k):_maxY;
						_maxY = (firstPt.y +k) > _maxY ? (firstPt.y +k):_maxY;
						_pointArray.push(new WebPDF.PDFPoint(firstPt.x -k, firstPt.y));
						_pointArray.push(new WebPDF.PDFPoint(firstPt.x-k, firstPt.y +k));
						_pointArray.push(new WebPDF.PDFPoint(firstPt.x, firstPt.y +k));
						_pointArray.push(new WebPDF.PDFPoint(firstPt.x +k, firstPt.y +k));
						_pointArray.push(new WebPDF.PDFPoint(firstPt.x +k, firstPt.y));
						_pointArray.push(new WebPDF.PDFPoint(firstPt.x +k, firstPt.y -k));
						_pointArray.push(new WebPDF.PDFPoint(firstPt.x, firstPt.y -k));
						_pointArray.push(new WebPDF.PDFPoint(firstPt.x -k, firstPt.y -k));
					}
					ptCount = _pointArray.length;
			}
			
			for(var i=0; i<ptCount; i++)
			{
				var pt =  _pointArray[i];
				pt.x -= (pageOffsetHtmlDoc.left - cavansOffset.left);
				pt.y -= (pageOffsetHtmlDoc.top - cavansOffset.top);
				var pdfPt = _pPageView.docPtToPDF(pt);
				pdfPtArray.push(pdfPt.x);
				pdfPtArray.push(pdfPt.y);
			}
			inkArray.push(pdfPtArray);
			var left = _minX - (pageOffsetHtmlDoc.left - cavansOffset.left);
			var width = _maxX - _minX;
			var top = _minY - (pageOffsetHtmlDoc.top - cavansOffset.top);
			var height = _maxY - _minY;
			var dt = new Date();
            var time = dt.valueOf() + dt.getTimezoneOffset() * 60000;
            var timestamp = (Math.floor(time / 1000)).toString();
            var annots_json = {
                c: "",
                ca: DEFAULT_OPCITY,
                cd: timestamp,
                cl: _hexColor,
                f: WebPDF.PDFData.AnnotFlag.ANNOTFLAG_PRINT,
                md: timestamp,
                n: timestamp,
                pop: null,
                subj: "Pencil",
                subty: "Ink",
                tit: _pReaderApp.getUserName(),
                ty: "Markup",
				ikl : inkArray,
				ikc: _hexColor,
				ikw: _width,
                rc: null
            };

            var pPDFPage = _pPageView.getPDFPage();
            var docRect = new WebPDF.PDFRect(left,top,left+width, top+height);
            var pdfrect = _pPageView.docToPDF(docRect,true);
            annots_json.rc = [(pdfrect.left-fDevitionX), (pdfrect.top+fDevitionY), (pdfrect.right+fDevitionX), (pdfrect.bottom-fDevitionY)];

            var popupRect = new WebPDF.PDFRect(0, 0, 0, 0);
            popupRect.top = pdfrect.top;
            var nPixelsPerPt =_pReaderApp.getPixelsPerPoint();
			var viewScale = _pPageView.getDocView().getScale();
            popupRect.left = (_pPageView.getPageViewWidth()-MKA_POPUPWIDTH*nPixelsPerPt)/(viewScale*nPixelsPerPt);
            popupRect.right = popupRect.left + MKA_POPUPWIDTH;
            popupRect.bottom = popupRect.top - MKA_POPUPHEIGHT;
			if(popupRect.bottom < 0)
			{
				WebPDF.RectUtils.offset(popupRect, 0, -popupRect.bottom);
			}

            annots_json.pop =
        	 {
        	     f: WebPDF.PDFData.AnnotFlag.ANNOTFLAG_PRINT,
        	     md: timestamp,
        	     n: timestamp + "_popup",
        	     op: 0,
        	     rc: [popupRect.left, popupRect.top, popupRect.right, popupRect.bottom]
        	 };

            var pAnnot = WebPDF.AnnotFactory.createAnnot(annots_json, false);
            
            pPDFPage.getAnnotJsonData().push(annots_json);
            pPDFPage.addAnnot(pAnnot);
            pPDFPage.setModified(true);
            var pageAnnotsDiv = _pBaseAnnotPlugin.getAnnotHandlerMgr().getUIManager().getPageAnnotsContainerID(_pPageView);
            var jqPageAnnotsDiv = $("#" + pageAnnotsDiv);
            var annotName = pAnnot.getAnnotName();
            var newAnnotHtml = _pBaseAnnotPlugin.getAnnotHandlerMgr().getUIManager().createAAnnotHtmlContent(_pPageView, pAnnot, annotName);
            jqPageAnnotsDiv.append(newAnnotHtml);
           // _pBaseAnnotPlugin.getAnnotHandlerMgr().bindPopupContextmenuEvent();
			_pBaseAnnotPlugin.getAnnotHandlerMgr().getAnnotHandlerByAnnot(pAnnot).onDraw(_pPageView,pAnnot,false);
			_pPageView.setModified(true);
			_pPageView.getDocView().setModified(true);
          //  _pBaseAnnotPlugin.setSaveCommentBtnState(true);
        };

		this.OnMouseUp = function(event)
		{
			if(_bMouseDown)
			{
					_self.CreateAnnot();
				_cavans2DContext.closePath();
			}
			_bMouseDown = false;

		

			return true;
		};

    };
    

});